package com.fauks.gmall.managerweb.utils;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.multipart.MultipartFile;

public class PmsUploadUtil {
    public static String uploadFile(MultipartFile file) {
        try {
            ClientGlobal.init(PmsUploadUtil.class.getResource("/tracker.conf").getPath());
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getTrackerServer();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            String fileName = file.getOriginalFilename();
            String[] uploadInfos = storageClient.upload_file(file.getBytes(), fileName.substring(fileName.lastIndexOf(".") + 1), null);
            StringBuilder mediaUrl = new StringBuilder("http://47.105.98.103");
            for (String uploadInfo : uploadInfos) {
                mediaUrl.append("/")
                        .append(uploadInfo);
            }
            return mediaUrl.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
